package models.entities;

import java.io.Serializable;
import java.util.List;

public class PaginatedList<T> implements Serializable {
    public List<T> items;
    public int pageNumber;
    public int pageSize;
    public long totalCount;
    public long pageCount;
    private int numbersLimit = 12;

    public PaginatedList(List<T> items) {
        this.items = items;
    }

    public PaginatedList() {
    }

    public PaginatedList(List<T> items, int totalCount) {
        this(items);
        this.totalCount = totalCount;
    }

    public PaginatedList(List<T> items, int totalCount, int pageSize) {
        this(items, totalCount);
        this.pageSize = pageSize;
    }

    public PaginatedList(List<T> items, long totalCount, int pageSize) {
        this(items, (int)totalCount, pageSize);
    }

    public PaginatedList(List<T> items, long totalCount) {
        this(items, (int)totalCount);
    }

    public long getPageCount() {
        long rem = totalCount % pageSize;
        pageCount = totalCount / pageSize;
        pageCount = rem > 0 ? pageCount + 1 : pageCount;
        return pageCount;
    }
    public long getPageCount(String pNumber){
        return getPageCount(pNumber != null ? Integer.parseInt(pNumber) : 0);
    }
    public long getPageCount(Integer pNumber) {
        long pc = getPageCount();
        int startNumberCount = 6;
        if (pc <= startNumberCount)
            return pc;

        int number;
        if (pNumber != null && pNumber != 0) {
            number = pNumber;
            startNumberCount = startNumberCount + number - 1;
            if (pc <= startNumberCount)
                return pc;
            return startNumberCount;
        } else {
            return startNumberCount;
        }
    }
    public int getStartPage(String pNumber){
        return getStartPage(pNumber != null ? Integer.parseInt(pNumber) : 1);
    }
    public int getStartPage(Integer pNumber) {
        int number = 1;
        if (pNumber != null && pNumber != 0) {
            number = pNumber;
            int midValue = (numbersLimit / 2) + 1;
            if (number <= midValue)
                return 1;
            else {
                return number - midValue;
            }
        }
        return 1;
    }
}

